<?php

namespace app\common\model\mysql;

use think\Collection;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

class AdminPermission extends BaseModel
{
    protected $schema=[
        'id'=>'int',
        'pid'=>'int',
        'name'=>'varchar',
        'icon'=>'varchar',
        'component'=>'varchar',
        'perms'=>'varchar',
        'menu_type'=>'varchar',
        'url'=>'varchar',
        'sort'=>'smallint',
        'is_show'=>'tinyint',
        'create_time'=>'int',
        'update_time'=>'int',
    ];
    /**
     * 获取权限数组对应的权限菜单
     * @param $rule
     * @return array|Collection
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getMenuListByRule($rule){
        $where=[
            ['perms','in',$rule],
            ['menu_type','in',['M','C']],
            ['is_show','=',1]
        ];
        return $this->where($where)->order('sort asc')->select();
    }

    /**
     * 获取所有菜单
     * @return array
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function getAllTree(): array
    {
        $list=$this->where([['is_show','=',1]])->select();
        return $list->isEmpty()?[]:$list->toArray();
    }
}
